package com.muyu.system.danmo.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.log.annotation.Log;
import com.muyu.system.danmo.dto.CloseDTO;
import com.muyu.system.danmo.dto.PayDTO;
import com.muyu.system.danmo.dto.RefundDTO;
import com.muyu.system.danmo.fegin.PayFeign;
import com.muyu.system.danmo.converter.PayMapper;
import com.muyu.system.danmo.service.PayService;
import com.muyu.system.danmo.vo.PayServiceVO;
import com.muyu.system.danmo.vo.PayVO;
import com.muyu.system.danmo.vo.RefundResultServiceVO;
import com.muyu.system.danmo.vo.RefundResultVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author Administrator
 */
@RestController
@Slf4j
@RequestMapping("/open/api")
@RequiredArgsConstructor
public class PayController implements PayFeign {

    private final PayService payService;

    private final PayMapper payMapper;


    /**
     * 支付
     * @param payDTO
     */
    @PostMapping("/pay")
    @Log(title = "支付==")
    public Result<PayVO> pay(@RequestBody @Valid PayDTO payDTO){
        PayServiceVO pay = payService.pay(payMapper.to(payDTO));
        return Result.success(payMapper.to(pay));
    }


    /**
     * 退款
     * @param refundDTO
     * @return
     */
    @PostMapping("/refund")
    @Log(title = "退款==")
    public Result<RefundResultVO> refund(@RequestBody @Valid RefundDTO refundDTO){
        RefundResultServiceVO refund = payService.refund(payMapper.to(refundDTO));
        return Result.success(payMapper.to(refund));
    }

    /**
     * 关闭订单
     * @param closeDTO
     * @return
     */
    @PostMapping("/close")
    public Result<String> close(@RequestBody @Valid CloseDTO closeDTO){
        payService.close(closeDTO);
        return Result.success("关闭成功!");
    }


}
